package com.lijiajia.cloud.common.security.access.expression.method;

import com.lijiajia.cloud.common.security.access.Authority;
import com.lijiajia.cloud.common.security.access.Role;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler;
import org.springframework.security.access.expression.method.MethodSecurityExpressionOperations;
import org.springframework.security.core.Authentication;

/**
 * description
 *
 * @author lijiajia
 * @since 2019-05-31
 */
public class CustomMethodSecurityExpressionHandler extends DefaultMethodSecurityExpressionHandler {
	private Role role;
	private Authority authority;

	/**
	 * 创建安全表达式root对象
	 */
	@Override
	protected MethodSecurityExpressionOperations createSecurityExpressionRoot(
		Authentication authentication, MethodInvocation invocation) {
		CustomMethodSecurityExpressionRoot root = new CustomMethodSecurityExpressionRoot(authentication, role, authority);
		root.setThis(invocation.getThis());
		root.setPermissionEvaluator(getPermissionEvaluator());
		root.setTrustResolver(getTrustResolver());
		root.setRoleHierarchy(getRoleHierarchy());
		root.setDefaultRolePrefix(getDefaultRolePrefix());

		return root;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public void setAuthority(Authority authority) {
		this.authority = authority;
	}
}
